package com.zzb.service.impl;

import com.zzb.DTO.NavigationDTO;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.NavigationVO;
import com.zzb.dao.NavigationDao;
import com.zzb.dao.impl.NavigationDaoImpl;
import com.zzb.entity.NavigationEntity;
import com.zzb.service.NavigationService;
import com.zzb.util.Tool;
import org.springframework.beans.BeanUtils;

import java.util.List;

public class NavigationServiceImpl implements NavigationService {
    NavigationEntity navigationEntity = new NavigationEntity();
    NavigationDao navigationDao = new NavigationDaoImpl();
    @Override
    public ResultDTO add(NavigationVO navigationVO) {
        //数据复制
        BeanUtils.copyProperties(navigationVO, navigationEntity);
        //判断名称是否存在
        if (navigationDao.findByName(navigationEntity) == null) {//不存在
            return ResultDTO.getResult("success", navigationDao.add(navigationEntity));
        } else {//已存在
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO delete(NavigationVO navigationVO) {
        //数据复制
        BeanUtils.copyProperties(navigationVO, navigationEntity);
        //判断类型是否存在
        if (navigationDao.findById(navigationEntity) != null) {//存在
            return ResultDTO.getResult("success", navigationDao.delete(navigationEntity));
        } else {//不存在
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO findById(NavigationVO navigationVO) {
        BeanUtils.copyProperties(navigationVO, navigationEntity);
        navigationEntity = navigationDao.findById(navigationEntity);
        if (navigationEntity != null) {
            NavigationDTO navigationDTO = new NavigationDTO();
            BeanUtils.copyProperties(navigationEntity, navigationDTO);
            return ResultDTO.getResult("success", navigationDTO);
        } else {
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO findAll(NavigationVO navigationVO) {
        BeanUtils.copyProperties(navigationVO, navigationEntity);
        int count = navigationDao.count(navigationEntity);
        List<NavigationEntity> list = navigationDao.findAll(navigationEntity);
        List<NavigationDTO> navigationDTOList = Tool.copy(list, NavigationDTO.class);
        return ResultDTO.getResult(count, navigationDTOList);
    }

    @Override
    public ResultDTO update(NavigationVO navigationVO) {
        BeanUtils.copyProperties(navigationVO, navigationEntity);
        NavigationEntity navigationEntityFindByName = navigationDao.findByName(navigationEntity);
        if (navigationEntityFindByName == null) {//类型名称不存在或者没有修改名字
            return ResultDTO.getResult("success", navigationDao.update(navigationEntity));
        }
        NavigationEntity navigationEntityFindById = navigationDao.findById(navigationEntity);
        String newName = navigationEntity.getName();
        String oldName = navigationEntityFindById.getName();
        if (oldName.equals(newName)) {
            return ResultDTO.getResult("success", navigationDao.update(navigationEntity));
        }
        return ResultDTO.getResult("error", 0);
    }
}
